﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using shop.Model;
using CommonTools;


namespace shop.Bll.BasicInfo
{

    public class JStockBll : BasicBllParent, IFastQueryBll<JStock>
    {
        /// <summary>
        /// 仓库基础资料 分类新增
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public override bool AddGroup(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JStock stock = obj as JStock;
            if (obj == null)
            {
                Msg = "b:转型失败";
                return false;
            }

            var id = DB.FSql.Insert(stock).ExecuteIdentity();
            stock.ID = (int)id;

            Msg = "保存成功";
            return true;
          
        }
        /// <summary>
        /// 仓库基础资料 分类删除
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public override bool DeleteGroup(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JStock stock = obj as JStock;
            if (obj == null)
            {
                Msg = "b:转型失败";
                return false;
            }
            if (stock.Code.Length <= 0)
            {
                Msg = "代码不能为空";
            }

            DB.FSql.Update<JStock>()
               .Where(a => a.Code == stock.Code && a.Isdel == false)
               .Set(a => a.Isdel, true)
               .ExecuteAffrows();

           
            Msg = "保存成功";
            return true;

        }


        /// <summary> 修改分组 </summary>
        public override bool UpdateGroup(object obj, IUser user, out string err)
        {
            err = "";
            if (!(obj is JStock model))
            {
                err = "辅助资料为空";
                return false;
            }


            //不能重名
            if (DB.FSql.Select<JStock>().Any(a=> a.Isdel== false && a.ID!=model.ID && a.Code == model.Code ))
            {
                err = "代码与其他项目的代码相同, 不能修改!";
                return false;
            }
            if (DB.FSql.Select<JStock>().Any(a => a.Isdel == false && a.ID != model.ID && a.Name == model.Name))
            {
                err = "名称与其他项目的名称相同, 不能修改!";
                return false;
            }


            DB.FSql.Update<JStock>()
                .Where(model)
                .Set(a => a.Name, model.Name)
                .ExecuteAffrows();

            err = null;
            return true;
        }

        /// <summary>
        /// 仓库基础资料 新增明细
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public override bool Insert(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JStock Stock = obj as JStock;
            Stock.Isdel = false;
            Stock.IsDetails = true;
            Stock.Biller = user.Name;

            if (obj == null)
            {
                Msg = "b:转型失败";
                return false;
            }
            if (Stock == null)
            {
                Msg = "b:仓库对象为空";
                return false;
            }
            if (Stock.Code == null || Stock.Code.Length <= 0)
            {
                Msg = "b:仓库代码不能为空";
                return false;
            }
            if (Stock.Name == null || Stock.Name.Length <= 0)
            {
                Msg = "b:仓库名称不能为空";
                return false;
            }

            if (DB.FSql.Select<JStock>().Any(a=> a.Code== Stock.Code && a.Isdel== false))
            {
                Msg = "b:仓库代码不能重复";
                return false;
            }

            if (DB.FSql.Select<JStock>().Any(a => a.Name == Stock.Name && a.Isdel == false))
            {
                Msg = "b:仓库代码不能重复";
                return false;
            }

            var id = DB.FSql.Insert(Stock).ExecuteIdentity();
            Stock.ID = (int)id;

            DB.FSql.Insert(new CLogs()
            {
                DocAction = "新增",
                DocName = nameof(JStock),
                DocId = Stock.ID.ToString(),
                Note = "新增仓库:" + Stock.Code + ", 名称:" + Stock.Name,
                Zdr = user.Name,
                Computer = user.ComputerInfo
            }).ExecuteAffrows();

            Msg = null;
            return false;
        }
        /// <summary>
        /// 仓库基础资料 删除明细
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public override bool Delete(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JStock Stock = obj as JStock;
            if (obj == null)
            {
                Msg = "b:转型失败";
                return false;
            }
            if (Stock.ID <= 0)
            {
                Msg = "b:仓库ID为空";
                return false;
            }
            if (Stock == null)
            {
                Msg = "b:仓库对象为空";
                return false;
            }
            if (Stock.Code == null || Stock.Code.Length <= 0)
            {
                Msg = "b:仓库代码不能为空";
                return false;
            }
            if (Stock.Name == null || Stock.Name.Length <= 0)
            {
                Msg = "b:仓库名称不能为空";
                return false;
            }

            DB.FSql.Update<JStock>()
                .Where(Stock)
                .Set(a => a.Isdel, true)
                .ExecuteAffrows();

            DB.FSql.Insert(new CLogs()
            {
                DocAction = "删除",
                DocName = nameof(JStock),
                DocId = Stock.ID.ToString(),
                Note = "删除仓库:" + Stock.Code + ", 名称:" + Stock.Name,
                Zdr = user.Name,
                Computer = user.ComputerInfo
            }).ExecuteAffrows();


            Msg = null;
            return false;
        }
        /// <summary>
        /// 仓库基础资料 查询
        /// </summary>
        /// <param name="code"></param>
        /// <param name="IsDetails"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public override IEnumerable<object> Query(string code, bool IsDetails, out string Msg)
        {
            Msg = null;

            var Stock = DB.FSql.Select<JStock>()
                .Where(s => s.IsDetails == IsDetails && s.Isdel == false);

            if (string.IsNullOrEmpty(code) == false)
            {
                Stock = Stock.Where(a => a.Code.StartsWith(code));
            }

            return Stock.ToList();
             
        }


        /// <summary>
        /// 仓库基础资料 修改明细
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public override bool Update(object obj, IUser user, out string Msg)
        {
            Msg = null;
            JStock Stock = obj as JStock;
            if (obj == null)
            {
                Msg = "b:转型失败";
                return false;
            }
            if (Stock.ID <= 0)
            {
                Msg = "b:仓库ID为空";
                return false;
            }
            if (Stock == null)
            {
                Msg = "b:仓库对象为空";
                return false;
            }
            if (Stock.Code == null || Stock.Code.Length <= 0)
            {
                Msg = "b:仓库代码不能为空";
                return false;
            }
            if (Stock.Name == null || Stock.Name.Length <= 0)
            {
                Msg = "b:仓库名称不能为空";
                return false;
            }

            DB.FSql.Update<JStock>()
            .SetSource(Stock)
            .ExecuteAffrows();
             
            Msg = null;
            return false;
        }


        #region 实现IFastQueryBll 接口, 在FastBox中使用

        /// <summary> 在查询,报表中显式的标题名称 </summary>
        public string Title => "配件仓库";

        /// <summary> 本地存储的明细 </summary>
        /// <remarks>如果基础资每次都从数据库查询, 这个没用, 保持null</remarks>
        public List<JStock> Items => null;


        /// <summary> 是否每次从数据库查询 </summary>
        public bool QueryFromDataBase => true;


        /// <summary> 存储在快速查询窗体中,显示的属性,及属性的显示, 这个暂时不用,以后删除 </summary>
        public Dictionary<string, string> DisplayColumns => null;


        /// <summary> 显示的列, 静态 </summary>
        private static readonly string[] _columns = new string[] { nameof(JStock.Code), nameof(JStock.Name) };


        /// <summary> 存储在快速查询窗体中,显示的属性 </summary>
        public string[] Columns => _columns;


        /// <summary> 实体模型的代码属性名称 </summary>
        public string CodeField => nameof(JStock.Code);



        /// <summary> 代码中级别的分隔符 </summary>
        public override string Separator => ".";



        /// <summary> 查询树形图的所有节点 </summary>
        public override IEnumerable<IFastTree> QueryTreeList()
        {
            var rst = DB.FSql.Select<JStock>().Where(a => a.IsDetails == false && a.Isdel == false);

            return rst.ToList().ToList<IFastTree>();
        }


        /// <summary> 查询方法 </summary>
        public List<JStock> Query(string findText, string prefixCode = null)
        {
            return DB.FSql.Select<JStock>()
                .Where(a => a.Isdel == false && (a.Name.Contains(findText) || a.Code.Contains(findText)))
               .ToList().OrderBy(a => a.Code).ToList();
        }


        /// <summary> 查询树形图的一个节点下的所有内容 </summary>
        public List<JStock> QueryByGroup(string group)
        {
            var rst = DB.FSql.Select<JStock>()
              .Where(w => w.IsDetails == true && w.Isdel == false && w.Code.StartsWith(group));

            return rst.ToList().OrderBy(a => a.Code).ToList();        //排序

        }

        public override bool Check(object obj, IUser user, out string err)
        {
            if (obj is JStock model)
            {
                DB.FSql.Update<JStock>()
                    .Where(model)
                    .Set(a => a.IsCheck, true)
                    .ExecuteAffrows();
            }

            err = null;
            return false;   
        }

        public override bool UnCheck(object obj, IUser user, out string err)
        {
            if (obj is JStock model)
            {
                DB.FSql.Update<JStock>()
                    .Where(model)
                    .Set(a => a.IsCheck, false)
                    .ExecuteAffrows();
            }

            err = null;
            return false;
        }


        #endregion


        #region 权限

        /// <summary> 查询导出权限 </summary>
        public override string LookRight => "01.03.1";

        /// <summary> 保存权限 </summary>
        public override string SaveRight => "01.03.2";

        /// <summary> 修改别人单据权限 </summary>
        public string ModifyRight => "01.03.3";

        /// <summary> 删除别人单据权限 </summary>
        public string DeleteRight => "01.03.4";

        /// <summary> 审核,反审核权限 </summary>
        public override string CheckRight => "01.03.5";


        #endregion



    }
}
